package com.yx.pc.api.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yx.common.exception.RRException;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.dao.TbRewardRecordDao;
import com.yx.modules.api.dao.TbUserMapper;
import com.yx.modules.api.entity.TbRewardRecord;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.service.PcTbUserService;
import com.yx.system.dao.TdBookMapper;
import com.yx.system.entity.TdBook;
import com.yx.utils.DateUtil;
import com.yx.utils.UidUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wuye on 2017/11/24.
 */
@Service
public class PcTbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements PcTbUserService {

    @Autowired
    private TbUserMapper userDao;

    @Autowired
    private TdBookMapper bookMapper;

    @Autowired
    private TbRewardRecordDao rewardRecordDao;

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser foundById(Long userId) {
        return userDao.selectByPrimaryKey(userId);
    }

    @Override
    public TbUser queryByMobile(String mobile) {
        return userDao.queryByMobile(mobile);
    }

    @Override
    public void deleteBatch(Long[] userIds) {
        List<Long> idsList = Arrays.stream(userIds).collect(Collectors.toList());
        userDao.deleteBatchIds(idsList);
    }

    @Override
    public long login(String mobile, String password) {
        TbUser user = queryByMobile(mobile);
        Assert.isNull(user, "手机号或密码错误");

        //密码错误
        if (!user.getPassword().equals(DigestUtils.sha256Hex(password))) {
            throw new RRException("手机号或密码错误");
        }

        return user.getUserId();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void save(String mobile, String password) {
        TbUser user = new TbUser();
        user.setMobile(mobile);
        user.setUsername(mobile);
        user.setPassword(DigestUtils.sha256Hex(password));
        user.setCreateTime(new Date());
        user.setUuid(UidUtil.getUUID());
        user.setSex(true);
        user.setBalance(0);
        user.setDescription("");
        user.setEnabled(true);
        user.setNickname(mobile);
        user.setProfileimageurl("http://39.108.8.150:8888/group1/M00/00/00/rBK42Vo7I7GAPvRSAABpt_SvtGE918.png");
        //设置用户身份为普通用户
        user.setIdentity(0);
        userDao.insert(user);
    }

    @Override
    public void update(TbUser tbuser) {
        userDao.updateByPrimaryKey(tbuser);
    }

    @Override
    public TbUser queryByUid(String value) {
        return userDao.queryByUid(value);
    }

    @Override
    public Page<TbUser> queryPageList(Page<TbUser> page, Map<String, Object> map) {
        page.setRecords(userDao.queryPageList(page, map));
        return page;
    }

    @Override
    public List<TbUser> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    public TbUser queryWBBangd(String wbtoken, String wbopenid) {
        return userDao.queryWBBangd(wbtoken, wbopenid);
    }

    @Override
    public TbUser queryQQBangd(String qqtoken, String qqopenid) {
        return userDao.queryQQBangd(qqtoken, qqopenid);
    }

    @Override
    public R updateUser(TbUser user, TbUser user1) {
        user.setProfileimageurl(user1.getProfileimageurl());
        user.setNickname(user1.getNickname());
        user.setEmail(user1.getEmail());
        user.setContactNumber(user1.getContactNumber());
        user.setSignature(user1.getSignature());
        user.setDescription(user1.getDescription());
        userDao.updateByPrimaryKey(user);
        return R.ok().put("data",userDao.selectByPrimaryKey(Long.valueOf(user.getUserId())));
    }

    @Override
    @Transactional
    public R rewardBook(TbUser user, int bookid, int actualMoney) {
        TdBook tdBook = bookMapper.selectByPrimaryKey(bookid);
        if (tdBook==null){
            return R.error("打赏的书籍不存在");
        }
        //设置扣下来的蝉币值
        user.setBalance(user.getBalance()-actualMoney);
        //更新用户
        userDao.updateByPrimaryKey(user);
        //增加书籍的打赏数和打赏金额
        tdBook.setRewardCount(tdBook.getRewardCount()+1);
        tdBook.setRewardMoney(tdBook.getRewardMoney()+actualMoney);
        //更新书籍信息
        bookMapper.updateByPrimaryKey(tdBook);
        //添加打赏记录
        TbRewardRecord tbRewardRecord = new TbRewardRecord();
        //注入属性
        Date date = new Date();
        tbRewardRecord.setUserId(user.getUserId());
        tbRewardRecord.setBookId(tdBook.getId());
        tbRewardRecord.setBookName(tdBook.getBookname());
        tbRewardRecord.setBookPic(tdBook.getPicurl());
        tbRewardRecord.setRewardMoney(actualMoney);
        tbRewardRecord.setRewardDate(date);
        tbRewardRecord.setRewardDay(DateUtil.getDay(date));
        tbRewardRecord.setRewardYear(DateUtil.getYear(date));
        tbRewardRecord.setRewardMonth(DateUtil.getMonth(date));
        tbRewardRecord.setRewardWeek(DateUtil.getWeek(date));
        //保存记录
        rewardRecordDao.save(tbRewardRecord);
        /*//找到图书对应的作者
        if (!tdBook.getAuthorCode().equals("0")){
            //根据作者code找到用户
            TbUser tbUser2 = tbUserMapper.findByAuthor(tdBook.getAuthorCode());
            if (tbUser2!=null){
                //增加作者账户的蝉币数量
                tbUser2.setBalance(tbUser2.getBalance()+actualMoney);
                //更新用户信息
                tbUserMapper.updateByPrimaryKey(tbUser2);
            }
        }*/
        return R.ok();
    }

    @Override
    public TbUser findByAuthor(String userId) {
        return userDao.findByAuthor(userId);
    }


}
